package com.nokia.pc3.core.format.input;

import java.io.ByteArrayOutputStream;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

import org.apache.hadoop.io.Writable;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

import com.nokia.pc3.core.dac.NokiaUmem;

/**
 * Nokia Transformer Record.
 * 
 * @author jcroke
 * 
 */
public class NokiaXFormRecord implements Writable {

	private NokiaUmem umem;

	protected NokiaXFormRecord() {
	}

	protected void init(NokiaUmem umem) {
		this.umem = umem;
	}

	@Override
	public void write(DataOutput out) {
		ByteArrayOutputStream buf = new ByteArrayOutputStream();
		ObjectMapper mapper = new ObjectMapper();
		try {
			mapper.writeValue(buf, umem.getOutput());
			String os = new String(buf.toByteArray());
			out.writeBytes(os);
		} catch (JsonGenerationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonMappingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void readFields(DataInput in) throws IOException {
		// not implemented
	}

	public NokiaUmem getUmem() {
		// TODO Auto-generated method stub
		return umem;
	}

	@Override
	public String toString() {
		ByteArrayOutputStream buf = new ByteArrayOutputStream();
		ObjectMapper mapper = new ObjectMapper();
		try {
			mapper.writeValue(buf, umem.getOutput());
		} catch (JsonGenerationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonMappingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return (new String(buf.toByteArray()));
	}

}
